#include "genetic.h"
#include "TSP.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

int main(int argc, char* argv[]){
  srand((unsigned int)(time(NULL)));
  int N,times,leng;
  double max;
  double Pr,Pc,Pm;
  std::cout << "number of city:" << std::endl;
  std::cin >> leng;
  std::cout << "group number:" << std::endl;
  std::cin >> N;
  std::cout << "number of iterations:" << std::endl;
  std::cin >> times;
  std::cout << "proportion of Pr,Pc and Pm:" << std::endl;
  std::cin >> Pr >> Pc >> Pm;
  //city* C=random_city(leng);
  //put_out_city(C,leng);
  city*C=load_rcity(leng);
  double startTime = clock();
  code* DNA=random_DNA(N,leng);
  double *record=new double[times+1];
  double *Eval=new double[N];
  max = d_max(DNA,C,N);
  for (int i=0; i<N ; i++)
    Eval[i]=DNA[i].fitness(C,leng,max);
  int r=std::distance(Eval, std::max_element(Eval, Eval + N));
  double f_max=DNA[r].total_dis(C,leng);
  code b_DNA=DNA[r];
  record[0]=f_max;
  std::cout<< record[0] <<std::endl;
  for (int j=0; j<times ;  j++){
    code* nDNA=new code[N];
    reproduce(DNA,nDNA,C,N,Pr,Pc,Pm);
    max = d_max(nDNA,C,N);
    for (int i=0; i<N ; i++)
      Eval[i]=nDNA[i].fitness(C,leng,max);
    r=std::distance(Eval, std::max_element(Eval, Eval + N));
    f_max=nDNA[r].total_dis(C,leng);
    record[j+1]=f_max;
    std::cout << record[j+1] <<std::endl;
    if (record[j+1] < b_DNA.total_dis(C,leng))
      b_DNA=nDNA[r];
    DNA=nDNA;
  }
  double endTime = clock();
  std::cout << "result:" << b_DNA.total_dis(C,leng) << std::endl;
  put_out(b_DNA);
  std::cout << "The run time is: " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << std::endl;
  return 0;
}
